/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

 Drupal.behaviors.ntuts_question = function(context) {
  $('a.q-link:not(.ntuts_question-processed)', context).addClass('ntuts_question-processed')
  .bind('click', function(){
	var qid = this.id.replace('question_','');
	var num_answer = parseInt($('#num_answer_'+qid).html());
	if (num_answer>15)
		return true;
	//if (num_answer==0)
	//	return false;
	if ($('.question_'+qid).html()=='')
		$.get('ajax_answer/'+ qid, null, anwserDetails);
	else
		$('.question_'+qid).slideToggle(500);
    return false;
  });
}

var anwserDetails = function(response) {
  var result = Drupal.parseJson(response);
  var qid = result.qid;
  $('.question_'+qid).html(result.data);
  $('.question_'+qid).slideToggle(500);
}

function replyQuestion(qid){
	var action = $('#form-qid-'+qid).attr('action');
	var dataMsg = {msg:$('#edit-answer-'+qid).val(),uid: $('#edit-uid-'+qid).val()};
	$.ajax({
        type: "POST",
        url: action,
		data: dataMsg,
        success: function(response){
		  var result = Drupal.parseJson(response);
          $('#form-qid-'+qid).before(result.data);
		  $('#edit-answer-'+qid).val('');
		}
	});
}

function deleteAnswer(id){
	if (!confirm("Are you sure you want to delete?"))
		return false;
	//var action = "ajax_delete_answer/"+id;
	var action = "answer/"+id+"/delete";
	$.ajax({
        type: "GET",
        url: action,
        success: function(response){
		  var result = Drupal.parseJson(response);
		  if (result.status==1)
			$('.ans-'+id).hide("slow", function(){ $(this).remove(); });
		  else
			alert(result.text);
		}
	});
	return false;
}
